const Router = require('koa-router')
// const validate = require('koa2-validation')
// const Joi = require('joi')
const tools = require('../utils/tools')
const Template = require('../schema/Template')
const Message = require('../schema/Message')

const templateRouter = new Router({
  prefix: '/recordApi/templates'
})

templateRouter.get('/doctor/all', async ctx => {
  let sub = tools.getSub(ctx.header.authorization);
  // const query = ctx.query;
  let tempMine = await Template.find({ templateOwner: sub.opCode })
  let tempDept = await Template.find({ templateOwner: sub.departId })
  let tempGroup = await Template.find({ templateOwner: sub.groups.map(i => i.groupId) })
  let tempHospital = await Template.find({ templateOwner: 'hospital' })
  ctx.body = {
    tempMine,
    tempGroup,
    tempDept,
    tempHospital
  }
})

templateRouter.get('/admin', async ctx => {
  // let sub = tools.getSub(ctx.header.authorization);
  const query = ctx.query;
  let templates;
  switch(query.templateOwnerType) {
    case 'doctor':
      templates = await Template.find({
        templateOwnerType: 'doctor',
        templateOwner: query.templateOwner
      })
      break
    case 'group':
      templates = await Template.find({
        templateOwnerType:  'group',
        templateOwner: query.templateOwner
      })
      break
    case 'dept':
      templates = await Template.find({
        templateOwnerType:  'dept',
        templateOwner: query.templateOwner
      })
      break
    case 'hospital':
      templates = await Template.find({
        templateOwnerType: 'hospital'
      })
      break
    default:
      templates = '参数不对'
  }
  ctx.body = templates
})

templateRouter.get('/one/:templateId', async ctx => {
  let templateId = ctx.params.templateId
  let template = await Template.findOne({ _id: templateId })
  ctx.body = template
})

templateRouter.post('/save', async ctx => {
  let template = ctx.request.body
  template.PE.values = JSON.stringify(template.PE.values)
  await Template.findOneAndUpdate({ _id: template._id }, template)
  ctx.body = {
    result: 'success'
  }
})

templateRouter.put('/new', async ctx => {
  let template = ctx.request.body
  template.PE.values = JSON.stringify(template.PE.values)
  await new Template(template).save()
  ctx.body = {
    result: 'success'
  }
})

templateRouter.del('/del/:id', async ctx => {
  let id = ctx.params.id
  await Template.deleteOne({ _id: id })
  ctx.body = {
    result: 'success'
  }
})

templateRouter.post('/share', async ctx => {
  let sub = tools.getSub(ctx.header.authorization)
  let body = ctx.request.body
  let template = await Template.findOne({ _id: body.templateId})
  let data = {
    type: 'msg',
    opName: sub.opName,
    templateId: body.templateId,
    templateName: template.name,
    remark: template.remark
  }
  let message = await new Message({
    opCode: body.shareTo,
    data: JSON.stringify(data)
  }).save()
  ctx.wss.clients.forEach(client => {
    if (client.user.opCode == body.shareTo) {
      client.send(JSON.stringify(message))
    }
  })
  ctx.body = {
    result: 'success'
  }
})

templateRouter.post('/accept', async ctx => {
  let sub = tools.getSub(ctx.header.authorization)
  let body = ctx.request.body
  let template = await Template.findOne({ _id: body.templateId })
  template = template.toObject()
  delete template._id
  delete template._v
  if (body.templateName.trim()) {
    template.name = body.templateName
  }
  if (body.remark.trim()) {
    template.remark = body.remark
  }
  template.templateOwner = sub.opCode
  template.templateOwnerName = sub.opName
  template.templateOwnerType = 'doctor'
  await new Template(template).save()

  ctx.body = {
    result: 'success'
  }
})

module.exports = templateRouter